/* eslint-disable indent, no-unused-vars */

const fs = require('fs')
const path = require('path')
const webpack = require('webpack')

const HtmlWebpackExternalsPlugin = require('./packages/html-webpack-externals-plugin')
const config = require('./config')

const resolve = path => require('path').join(__dirname, path)

module.exports = {
  pages: config.getActivedPages(),
  outputDir: config.outputDir,
  publicPath: process.env.NODE_ENV !== 'development' ? config.publicPath : '/',
  runtimeCompiler: true,
  productionSourceMap: false,
  devServer: {
    host: '0.0.0.0',
    port: 9103,
    publicPath: '/',
    index: config.index,
    disableHostCheck: true,
    https: config.https && {
      ca: fs.readFileSync('cert/ca.crt'),
      key: fs.readFileSync('cert/server.key'),
      cert: fs.readFileSync('cert/server.crt')
    },
    proxy: {
      '/api': {
        target: 'http://domain.com/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  },
  chainWebpack: config => {
    /**
     * CDN设置
     */
    const names = ['postcss', 'css', 'scss', 'sass', 'stylus', 'less']
    const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
    if (process.env.VUE_APP_PUBLIC_PATH) {
      names.forEach(name => {
        types.forEach(type => {
          // 修改样式中资源路径的前缀
          config.module
            .rule(name)
              .oneOf(type)
                .use('extract-css-loader')
                  .tap(options => {
                    options.publicPath = process.env.VUE_APP_PUBLIC_PATH
                    return options
                  })
        })
      })
      config.plugin('html-webpack-externals')
        .use(HtmlWebpackExternalsPlugin, [
          require('./dependencies-cdn.json')
        ])
        .tap(args => {
          args[0].enabled = true
          args[0].publicPath = process.env.VUE_APP_PUBLIC_PATH
          return args
        })
    }
    config.plugin('copy')
      .tap(([patterns, options = {}]) => {
        options.ignore = options.ignore || []
        options.ignore = [
          'index.html', // 忽略public/index.html
          ...options.ignore
        ]
        return [patterns, options]
      })
    config.plugin('provide')
      .use(webpack.ProvidePlugin, [{
        // $: 'jquery'
      }])
    // 移除eslint
    config.module.rules.delete('eslint')
    // 导入样式资源
    types.forEach(type => {
      config.module
        .rule('scss')
          .oneOf(type)
            .use('style-resources')
              .loader('style-resources-loader')
              .options({
                patterns: [
                  resolve('src/assets/styles/mixins.scss')
                ]
              })
    })
  },
  configureWebpack: {
    resolve: {
      extensions: ['.js', '.vue'],
      alias: {
        '@': resolve('src'),
        '@page': resolve(`src/pages/${process.env.VUE_APP_PAGE_NAME}`)
      }
    },
    optimization: {
      // minimize: false
    }
  },
  css: {
    loaderOptions: {
      // scss: {
      //   data: `
      //     @import "@/assets/styles/mixins.scss";
      //   `
      // }
    }
  }
}
